package com.institute.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.institute.entity.Institute;
import com.institute.entity.Position;


public class PositionDTO {
	private Long id;
	private String name;
	private String code;
	private String department;
	private String description;
	private Integer capacity;
	private Integer staffNum;
	private Long fatherId;

	public PositionDTO() {
		super();
	}
	
	public PositionDTO(Position position,String department,Integer staffNum) {
		super();
		this.id = position.getId();
		this.name = position.getName();
		this.description = position.getDescription();
		this.capacity = position.getCapacity();
		this.department = department;
		this.staffNum = staffNum;
		this.code = position.getCode();
	}
	
	
	public Long getFatherId() {
		return fatherId;
	}

	public void setFatherId(Long fatherId) {
		this.fatherId = fatherId;
	}

	public Long getId() {
		return id;
	}
	
	public String getCode() {
		return code;
	}

	public String getName() {
		return name;
	}
	public String getDepartment() {
		return department;
	}
	public String getDescription() {
		return description;
	}
	public Integer getCapacity() {
		return capacity;
	}
	public Integer getStaffNum() {
		return staffNum;
	}

	
	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setCapacity(Integer capacity) {
		this.capacity = capacity;
	}

	public void setStaffNum(Integer staffNum) {
		this.staffNum = staffNum;
	}

	public static Specification<Position> getSpecification( final PositionDTO positionDTO,Institute dept)
	{
		Specification<Position> spec = new Specification<Position>() {
			public Predicate toPredicate(Root<Position> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != positionDTO && !StringUtils.isEmpty(positionDTO.getName())) {
					 Predicate  p1 =  cb.like(root.get("name").as(String.class),"%"+positionDTO.getName()+"%");
					 list.add(p1);
				 }
				 if(null != positionDTO && !StringUtils.isEmpty(positionDTO.getCode())) {
					 Predicate  p3 =  cb.like(root.get("code").as(String.class),"%"+positionDTO.getCode()+"%");
					 list.add(p3);
				 }
				 if(null != dept) {
					 Predicate  p3 =  cb.equal(root.get("dept").as(Institute.class),dept);
					 list.add(p3);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
